<template>
	<view class="container">
		<!-- <uni-card :is-shadow="false" is-full>
			<text class="uni-h6">uni-forms 组件一般由输入框、选择器、单选框、多选框等控件组成，用以收集、校验、提交数据。</text>
		</uni-card> -->
		<uni-section title="意愿下沉医疗机构" type="line">
			<view class="example">
				<!-- 基础用法，不包含校验规则 -->
				<uni-forms ref="baseForm" :modelValue="baseFormData" >
					<uni-group mode="card" :title="'意向服务机构 '+(index+1)" v-for="(item,index) in baseFormData.institutions" :key="item.id">
						<template v-slot:title>
							<uni-list >
								<uni-list-item :rightText="'✖'"  :title="'意向服务机构 '+(index+1)" :clickable="true" @click="delInstitution(index)"/>
							</uni-list>
						</template>
				<!-- 		<uni-forms-item label="意向区县" required class="cst-form-item" :labelWidth='120'>
							<uni-data-picker v-model="item.area" :localdata="areaOptions" 
							popup-title="请选择意向区县" :border="false" placeholder="请选择意向区县" @nodeclick="getInstitutionList"></uni-data-picker>
						</uni-forms-item> -->
						<uni-forms-item label="意向服务机构" required class="cst-form-item" :labelWidth='120' 
						:rules="[{'required': true,errorMessage: '请选择意向服务机构'}]" :name="['institutions',index,'institution']">
							<uni-data-picker v-model="item.institution" :map="{text:'text',value:'code'}" :localdata="institutionOptions" 
							popup-title="请选择意向机构"  :border="false" placeholder="请选择意向机构" ></uni-data-picker>
						</uni-forms-item>
						<uni-forms-item label="意向服务年限(年)" required class="cst-form-item" :labelWidth='120' 
						:rules="[{'required': true,errorMessage: '请输入意向年限(年)'}, {'format': 'number', errorMessage: '意向服务年限仅能输入数值'}]" :name="['institutions',index,'duration']">
							<uni-easyinput v-model="item.duration" placeholder="请输入意向年限" :inputBorder="false"/>
						</uni-forms-item>
					</uni-group>
					<uni-group v-if="baseFormData.institutions.length < 5">
						<button  type="default" plain="true" @click="addInstitution" >✚ 增加意向下沉机构</button>
					</uni-group>
					<uni-forms-item label="是否愿意在选择意向下沉医疗机构范围内服从调剂" required class="cst-form-item" 
					:rules="[{'required': true,errorMessage: '请选择是否愿意'}]" :labelWidth='180' name="acceptAdjustment">
						<uni-data-checkbox v-model="baseFormData.acceptAdjustment" :localdata="acceptAdjustmentOptions" />
					</uni-forms-item>
					<view class="uni-btn-v">
						<button  @click="uploadUserInfo('baseForm')" type="primary">提交</button>
					</view>
				</uni-forms>
			</view>
		</uni-section>
	</view>
</template>

<script>
	import apiConfig  from "/api/config.js"
	import CryptoJS from 'crypto-js'
	export default {
		data() {
			return {
				areaOptions: [
					{ value: "420100", text: "武汉市", children: [
						{ value: "420102", text: "江岸区"},
						{ value: "420103", text: "江汉区"},
						{ value: "420104", text: "硚口区"},
						{ value: "420105", text: "汉阳区"},
						{ value: "420106", text: "武昌区"},
						{ value: "420107", text: "青山区"},
						{ value: "420110", text: "东湖风景区"},
						{ value: "420111", text: "洪山区"},
						{ value: "420112", text: "东西湖区"},
						{ value: "420113", text: "汉南区"},
						{ value: "420114", text: "蔡甸区"},
						{ value: "420115", text: "江夏区"},
						{ value: "420116", text: "黄陂区"},
						{ value: "420117", text: "新洲区"},
						{ value: "420120", text: "东湖高新区"},
						{ value: "420130", text: "长江新区"},
						{ value: "420199", text: "市直管"},]},
					{ value: "420200", text: "黄石市", children: [
						{ value: "420202", text: "黄石港区"},
						{ value: "420203", text: "西塞山区"},
						{ value: "420204", text: "下陆区"},
						{ value: "420205", text: "铁山区"},
						{ value: "420222", text: "阳新县"},
						{ value: "420271", text: "开发区·铁山区"},
						{ value: "420281", text: "大冶市"},
						{ value: "420299", text: "市直管"},]},
					{ value: "420300", text: "十堰市", children: [
						{ value: "420301", text: "武当山特区(虚拟)"},
						{ value: "420302", text: "茅箭区"},
						{ value: "420303", text: "张湾区"},
						{ value: "420304", text: "郧阳区"},
						{ value: "420310", text: "市直管"},
						{ value: "420322", text: "郧西县"},
						{ value: "420323", text: "竹山县"},
						{ value: "420324", text: "竹溪县"},
						{ value: "420325", text: "房县"},
						{ value: "420381", text: "丹江口市"},
						{ value: "420385", text: "经济开发区"},]},
					{ value: "420500", text: "宜昌市", children: [
						{ value: "420502", text: "西陵区"},
						{ value: "420503", text: "伍家岗区"},
						{ value: "420504", text: "点军区"},
						{ value: "420505", text: "猇亭区"},
						{ value: "420506", text: "夷陵区"},
						{ value: "420510", text: "市直管"},
						{ value: "420511", text: "高新区"},
						{ value: "420512", text: "葛洲坝"},
						{ value: "420525", text: "远安县"},
						{ value: "420526", text: "兴山县"},
						{ value: "420527", text: "秭归县"},
						{ value: "420528", text: "长阳土家族自治县"},
						{ value: "420529", text: "五峰土家族自治县"},
						{ value: "420581", text: "宜都市"},
						{ value: "420582", text: "当阳市"},
						{ value: "420583", text: "枝江市"},]},
					{ value: "420600", text: "襄阳市", children: [
						{ value: "420602", text: "襄城区"},
						{ value: "420606", text: "樊城区"},
						{ value: "420607", text: "襄州区"},
						{ value: "420609", text: "市直管"},
						{ value: "420624", text: "南漳县"},
						{ value: "420625", text: "谷城县"},
						{ value: "420626", text: "保康县"},
						{ value: "420682", text: "老河口市"},
						{ value: "420683", text: "枣阳市"},
						{ value: "420684", text: "宜城市"},]},
					{ value: "420700", text: "鄂州市", children: [
						{ value: "420702", text: "梁子湖区"},
						{ value: "420703", text: "华容区"},
						{ value: "420704", text: "鄂城区"},
						{ value: "420799", text: "市直管"},]},
					{ value: "420800", text: "荆门市", children: [
						{ value: "420802", text: "东宝区"},
						{ value: "420804", text: "掇刀区"},
						{ value: "420822", text: "沙洋县"},
						{ value: "420881", text: "钟祥市"},
						{ value: "420882", text: "京山市"},
						{ value: "420885", text: "屈家岭"},
						{ value: "420886", text: "漳河新区"},
						{ value: "420887", text: "沙洋监狱管理局"},
						{ value: "420899", text: "市直管"},]},
					{ value: "420900", text: "孝感市", children: [
						{ value: "420902", text: "孝南区"},
						{ value: "420910", text: "市直管"},
						{ value: "420921", text: "孝昌县"},
						{ value: "420922", text: "大悟县"},
						{ value: "420923", text: "云梦县"},
						{ value: "420981", text: "应城市"},
						{ value: "420982", text: "安陆市"},
						{ value: "420984", text: "汉川市"},
						{ value: "420997", text: "临空区"},
						{ value: "420998", text: "高新区"},
						{ value: "420999", text: "市直管"}]},
					{ value: "421000", text: "荆州市", children: [
						{ value: "421002", text: "沙市区"},
						{ value: "421003", text: "荆州区"},
						{ value: "421022", text: "公安县"},
						{ value: "421024", text: "江陵县"},
						{ value: "421081", text: "石首市"},
						{ value: "421083", text: "洪湖市"},
						{ value: "421087", text: "松滋市"},
						{ value: "421088", text: "监利市"},
						{ value: "421098", text: "开发区"},
						{ value: "421099", text: "市直管"},]},
					{ value: "421100", text: "黄冈市", children: [
						{ value: "421102", text: "黄州区"},
						{ value: "421121", text: "团风县"},
						{ value: "421122", text: "红安县"},
						{ value: "421123", text: "罗田县"},
						{ value: "421124", text: "英山县"},
						{ value: "421125", text: "浠水县"},
						{ value: "421126", text: "蕲春县"},
						{ value: "421127", text: "黄梅县"},
						{ value: "421181", text: "麻城市"},
						{ value: "421182", text: "武穴市"},
						{ value: "421185", text: "龙感湖区"},
						{ value: "421199", text: "市直管"},]},
					{ value: "421200", text: "咸宁市", children: [
						{ value: "421202", text: "咸安区"},
						{ value: "421221", text: "嘉鱼县"},
						{ value: "421222", text: "通城县"},
						{ value: "421223", text: "崇阳县"},
						{ value: "421224", text: "通山县"},
						{ value: "421281", text: "赤壁市"},
						{ value: "421299", text: "市直管"},]},
					{ value: "421300", text: "随州市", children: [
						{ value: "421301", text: "随州市直"},
						{ value: "421303", text: "曾都区"},
						{ value: "421321", text: "随县"},
						{ value: "421381", text: "广水市"},]},
					{ value: "422800", text: "恩施土家族苗族自治州", children: [
						{ value: "422801", text: "恩施市"},
						{ value: "422802", text: "利川市"},
						{ value: "422822", text: "建始县"},
						{ value: "422823", text: "巴东县"},
						{ value: "422825", text: "宣恩县"},
						{ value: "422826", text: "咸丰县"},
						{ value: "422827", text: "来凤县"},
						{ value: "422828", text: "鹤峰县"},
						{ value: "422899", text: "市直管"},]},
					{ value: "429000", text: "省直辖县级行政单位", children: [
						{ value: "429004", text: "仙桃市"},
						{ value: "429005", text: "潜江市"},
						{ value: "429006", text: "天门市"},
						{ value: "429021", text: "神农架林区"},]}
				],
				institutionOptions:[],
				acceptAdjustmentOptions:[
						{ value: "1", text: "是" },
						{ value: "2", text: "否" },
				],
				institutionList:[],
				// 基础表单数据
				baseFormData: {
					institutions:[
						{
							institution:'',
							duration:''
						}
					],
					personInfo:{},
					acceptAdjustment:''
				},
			}
		},
		onLoad() {
			// 加载字典
			this.loadInstitutionOptions();
			// 获取个人下沉信息
			this.getUserInfo();
		},
		onReady() {
			// 设置自定义表单校验规则，必须在节点渲染完毕后执行
			// this.$refs.customForm.setRules(this.customRules)
		},
		methods: {
			loadInstitutionOptions(){
				uni.request({
					url: apiConfig.api_host + "/wsb/interface/txywryxc.do?action=getDictionaryData&dictionaryName=DIM_YYMC" ,
					success: data => {
						var decrypt = this.aes_decrypt(data.data)
						var rstData = JSON.parse(decrypt.replace(/[^\x20-\x7E\u4E00-\u9FFF]+/g, ""));
						console.log("机构信息", rstData.data.data);
						this.institutionOptions = rstData.data.data;
					},
					fail: (data, code) => {
						console.log('fail', data);
					}
				});
			},
			getUserInfo(){
				uni.request({
					url: apiConfig.api_host + "/wsb/interface/txywryxc.do?action=getUserInfo&sign=" + this.$sign,
					success: data => {
						var decrypt = this.aes_decrypt(data.data)
						var rstData1 = JSON.parse(decrypt.replace(/[^\x20-\x7E\u4E00-\u9FFF]+/g, ""));
						console.log("个人信息", rstData1);
						if(rstData1.code == 200){
							let rstData = rstData1.data;
							this.baseFormData.acceptAdjustment = rstData.SFFCTJ;
							this.baseFormData.institutions = this.convertInstitution(rstData)
							if(rstData.realname){
								rstData.XM = rstData.realname;
								delete rstData.realname;
							}
							if(rstData.BHYY){
								delete rstData.BHYY;
							}
							this.baseFormData.personInfo = rstData;
						}
					},
					fail: (data, code) => {
						console.log('fail', data);
					}
				});
			},
			convertInstitution: function(rstData){
				let ins = [];
				if(rstData.YXJG1){
					ins.push({
						institution: rstData.YXJG1,
						duration: rstData.YXFWQX1
					})
				}
				if(rstData.YXJG2){
					ins.push({
						institution: rstData.YXJG2,
						duration: rstData.YXFWQX2
					})
				}
				if(rstData.YXJG3){
					ins.push({
						institution: rstData.YXJG3,
						duration: rstData.YXFWQX3
					})
				}
				if(rstData.YXJG4){
					ins.push({
						institution: rstData.YXJG4,
						duration: rstData.YXFWQX4
					})
				}
				if(rstData.YXJG5){
					ins.push({
						institution: rstData.YXJG5,
						duration: rstData.YXFWQX5
					})
				}
				if(ins.length == 0){
					ins.push({
							institution:null,
							duration:null
						});
				} 
				return ins;
			},
			addInstitution: function(){
				this.baseFormData.institutions.push({
					institution:'',
					duration:''
				})
			},
			delInstitution: function(index){
				if(this.baseFormData.institutions.length==1){
					uni.showToast({
						title: '至少要填写一条下沉机构',
						duration: 2000,
					});
				}else {
					console.log('remove', index)
					this.baseFormData.institutions.splice(index, 1)
				}
			},
			packageInstitution(){
				this.baseFormData.personInfo.YXJG1 = null;
				this.baseFormData.personInfo.YXFWQX1 = null;
				this.baseFormData.personInfo.YXJG2 = null;
				this.baseFormData.personInfo.YXFWQX2 = null;
				this.baseFormData.personInfo.YXJG3 = null;
				this.baseFormData.personInfo.YXFWQX3 = null;
				this.baseFormData.personInfo.YXJG4 = null;
				this.baseFormData.personInfo.YXFWQX4 = null;
				this.baseFormData.personInfo.YXJG5 = null;
				this.baseFormData.personInfo.YXFWQX5 = null;
				console.log('package', this.baseFormData.institutions)
				this.baseFormData.institutions.forEach((item, index) => {
					if(index == 0){
						this.baseFormData.personInfo.YXJG1 = item.institution;
						this.baseFormData.personInfo.YXFWQX1 = item.duration;
					}
					if(index == 1){
						this.baseFormData.personInfo.YXJG2 = item.institution;
						this.baseFormData.personInfo.YXFWQX2 = item.duration;
					}
					if(index == 2){
						this.baseFormData.personInfo.YXJG3 = item.institution;
						this.baseFormData.personInfo.YXFWQX3 = item.duration;
					}
					if(index == 3){
						this.baseFormData.personInfo.YXJG4 = item.institution;
						this.baseFormData.personInfo.YXFWQX4 = item.duration;
					}
					if(index == 4){
						this.baseFormData.personInfo.YXJG5 = item.institution;
						this.baseFormData.personInfo.YXFWQX5 = item.duration;
					}
				})
				this.baseFormData.personInfo.SFFCTJ =  this.baseFormData.acceptAdjustment;
				
				return this.baseFormData.personInfo;
			},
			uploadUserInfo(ref) {
				this.$refs[ref].validate().then(res => {
					console.log('success', res, JSON.stringify(this.packageInstitution()));
					var encrypt = this.aes_encrypt(JSON.stringify(this.packageInstitution()))
					// console.log('===encrypt===',encrypt);
					// var decrypt = this.aes_decrypt(encrypt)
					// console.log('===decrypt===',JSON.parse(decrypt));
					
					// 提交中
					uni.showLoading({title: '提交中', mask: true });
					uni.request({
						url: apiConfig.api_host + "/wsb/interface/txywryxc.do?action=uploadUserInfo&type=org&sign="+ this.$sign + "&data=" +encodeURIComponent(encodeURIComponent(this.aes_encrypt(JSON.stringify(this.baseFormData.personInfo)))),
						// data: {data: aes_encrypt(JSON.stringify(this.baseFormData.personInfo))},
						// method:'POST',
						success: data => {
							uni.hideLoading();
							console.log("更新用户返回结果", data);
							var resp = JSON.parse(this.aes_decrypt(data.data).replace(/[^\x20-\x7E\u4E00-\u9FFF]+/g, ""));
							console.log("数据解密后", resp);
							if(resp.code == 200){
								uni.showModal({
										// title: '',
										content: '提交成功。',
										showCancel:false,
										success: res => {
											console.log('res', res)
											if (res.confirm) {
												uni.switchTab({
													url: '/pages/retire/home/sinkList',
												});
											}
										}
								})
							}
						},
						fail: (data, code) => {
							console.log('fail', data);
						}
					});
				}).catch(err => {
					console.log('err', err);
				})
			},
			aes_encrypt:function(word){
				let keyStr = "abc654321abc===+";//加密密钥
				let ivStr  = "abc654321abc===+";//矢量
			    keyStr = keyStr ? keyStr : 'hfhfuu8dolv98bt6';
			    var key  = CryptoJS.enc.Utf8.parse(keyStr);
			    var iv   = CryptoJS.enc.Utf8.parse(ivStr);
			    // var srcs = CryptoJS.enc.Utf8.parse(word);
			    var encrypted = CryptoJS.AES.encrypt(word, key, {
			        iv:iv,
			        mode:CryptoJS.mode.CBC,
			        padding: CryptoJS.pad.Pkcs7});
			    return encrypted.toString();
			},
			 
			//解密  
			aes_decrypt:function(word){  
				let keyStr = "abc654321abc===+";//加密密钥
				let ivStr  = "abc654321abc===+";//矢量
			    keyStr = keyStr ? keyStr : 'hfhfuu8dolv98bt6';
			    var key  = CryptoJS.enc.Utf8.parse(keyStr);//
			    var iv   = CryptoJS.enc.Utf8.parse(ivStr);
			    // const restoreBase64 = word.replace(/\-/g,'+').replace(/_/g,'/');
			    var restoreBase64=word.replace(/[\r\n]/g,'');
			 
			    var decrypt = CryptoJS.AES.decrypt(restoreBase64, key, {
			        iv:iv,
			        mode:CryptoJS.mode.CBC,
			        padding: CryptoJS.pad.ZeroPadding});
			    return CryptoJS.enc.Utf8.stringify(decrypt).toString();
			}
		}
	}
</script>

<style lang="scss" scoped>
	.example {
		padding: 15px;
		background-color: #fff;
	}

	.segmented-control {
		margin-bottom: 15px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		margin-left: 10px;
	}
	.cst-form-item {
		border-bottom:0px #e5e5e5 solid;
	}
</style>
